package org.jaaksi.kottlindemo.test

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import org.jaaksi.kottlindemo.dao.DbUtil
import org.jaaksi.kottlindemo.dao.model.db.Note
import org.jaaksi.kottlindemo.dao.model.db.User
import org.jaaksi.kottlindemo.databinding.ActivityTestDbBinding
import javax.inject.Inject

@AndroidEntryPoint
class TestDbActivity : AppCompatActivity() {
    private val binding by lazy { ActivityTestDbBinding.inflate(layoutInflater) }

    @Inject
    lateinit var dbUtil: DbUtil

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        init()
    }

    private fun init() {
        binding.apply {
            btnInsert.setOnClickListener {
                // 开启事务
                dbUtil.callInTx {
                    val userCode = etCode.text.trim().toString()
                    dbUtil.addUser(
                        User(
                            null,
                            etName.text.trim().toString(),
                            userCode
                        )
                    )

                    dbUtil.addNote(
                        Note(null, userCode, etNote.text.trim().toString())
                    )
                }


            }

            btnQuery.setOnClickListener {
                dbUtil.getUserList()
                val userBean = dbUtil.getUserByCode("1002")
                println(">> $userBean")
            }
        }
    }
}